package br.com.amadeus.android.activities.course.task;

import java.util.ArrayList;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.adapters.TaskAdapter;
import br.com.amadeus.android.adapters.TaskList;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Action;
import br.com.amadeus.android.entities.Resource;
import br.com.amadeus.android.entities.Task;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class ListTaskActivity extends Activity{
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		finish();
	}

	private static final int NOVO = 0;
	private static final int LISTAGEM = 1;
	private static final int CALENDAR = 2;
	
	private ArrayList<TaskList> taskListAdapter;
	
	FacadeAmadeusDroid facade = FacadeAmadeusDroid.getInstance(this);
	
	Intent itTaskForm;
	Intent itTaskCalendar;
	
	int course_id;
	int goal_id;
	
	private ListView listView;
	
	public void onCreate(Bundle savedInstanceState){ 
		super.onCreate(savedInstanceState);
        setContentView(R.layout.amadeus_list_task_activity);
        
        Bundle gotBasket = getIntent().getExtras();
        course_id = gotBasket.getInt("course_id");
        goal_id = gotBasket.getInt("goal_id");
        
        /*Carregando os objetos no arraylist*/
        
        ArrayList<Task> tasks = facade.getAllTasksByGoal(goal_id);
        taskListAdapter = new ArrayList<TaskList>();
        if( !tasks.isEmpty()){
	        for (int i = 0; i < tasks.size(); i++) {
	        	Action action = facade.getAction(tasks.get(i).getAction().getId());
	        	Resource resource = facade.getResource(tasks.get(i).getResource().getId());
	        	TaskList taskList = new TaskList(tasks.get(i).getId(), action.getValue()+" "+resource.getName(), R.id.imgBtnExcluir, R.id.imgBtnEditar);
				taskListAdapter.add(taskList);
			}
        }else{
        	Toast.makeText(this, "Nenhuma Tarefa Cadastrada!", Toast.LENGTH_LONG);
        }
        //instanciando o baseAdapter chamado ObjetivoAdapter
        AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
        TaskAdapter adapter = new TaskAdapter(this, taskListAdapter, alarme, goal_id);

        this.listView = (ListView) findViewById(R.id.lvTasks);
        this.listView.setAdapter(adapter);
        
	}
	
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		MenuItem itemNovo = menu.add(0, NOVO, 0, "Novo");
		itemNovo.setIcon(R.drawable.novoitem);
		
		MenuItem itemList = menu.add(0,LISTAGEM, 0, "Listagem");
		itemList.setIcon(R.drawable.listagem);
		
		MenuItem itemCalendar = menu.add(0, CALENDAR, 0, "Calendário");
		itemCalendar.setIcon(R.drawable.calendar);
		
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch (item.getItemId()) 
		{
			case NOVO:
				itTaskForm = new Intent("FORM_TASK");
				Bundle basket = new Bundle();
				basket.putInt("goal_id", goal_id);
				basket.putInt("course_id", course_id);
				itTaskForm.putExtras(basket);
				startActivity(itTaskForm);
				return true;
	
			case LISTAGEM:				
				return true;
		
			case CALENDAR:
				itTaskCalendar = new Intent("CALENDAR_TASK");
				startActivity(itTaskCalendar);
				return true;
		}
		return false;
	}
}
